package com.supcon.exchangedata.service;


import com.supcon.exchangedata.pojo.entity.oracle.CtKpiDay;
import com.supcon.exchangedata.pojo.entity.oracle.StatistOtherParametersDay;
import com.supcon.exchangedata.pojo.entity.mysql.operation.hj.GarbageClean;
import com.supcon.exchangedata.repository.master.*;
import com.supcon.exchangedata.repository.slave.SlaveGarbageCleanRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * 垃圾清运量，包含指标 垃圾清运量   九峰转运量--40005 总其他垃圾量 总厨余垃圾量 总餐厨垃圾量 40004
 *
 * @Author: yangbin
 * @Date: 2022/03/18/9:22
 * @Description:
 */
@Service
@Slf4j
public class GarbageCleanService {
    @Resource
    private SlaveGarbageCleanRepository slaveGarbageCleanRepository;
    @Resource
    private MasterKpiHourRepository masterKpiHourRepository;
    @Resource
    private MasterKpiMonthRepository masterKpiMonthRepository;
    @Resource
    private MasterKpiDayRepository masterKpiDayRepository;
    @Resource
    private MasterStatisticalRealTimeDataRepository masterStatisticalRealTimeDataRepository;
    @Resource
    private MasterStatistOtherParametersDayRepository masterStatistOtherParametersDayRepository;
    @Resource
    private MasterStatistOtherParametersHourRepository masterStatistOtherParametersHourRepository;
    @Resource
    private MasterStatistOtherParametersMonthRepository masterStatistOtherParametersMonthRepository;

    /**
     * 垃圾清运量   九峰转运量--40005 总其他垃圾量 总厨余垃圾量 总餐厨垃圾量 40004
     */
    public void getGarbageCleanData() {

        //这些指标都是保存到天表
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate now = LocalDate.now();
        String endTime = dateTimeFormatter.format(now);
        LocalDate localDate = now.minusMonths(1L);
        String startTime = dateTimeFormatter.format(localDate);
        List<GarbageClean> all = this.slaveGarbageCleanRepository.findAllByDayAfterAndDayBeforeOrderByDayDesc(startTime, endTime);
       /* List<GarbageClean> all = slaveGarbageCleanRepository.findAll();*/
        if (!CollectionUtils.isEmpty(all)) {
            all.forEach((e) -> {
                //获取日期
                String day = e.getDay();
                //垃圾清运量
                String totalAmount = e.getTotalAmount();
                saveOrUpdateKpiDay("10040018", day, totalAmount);
                saveOrUpdateStatistOtherParametersDay(4L, 40031L, day, totalAmount);
                //九峰转运量
                String jiuFengAmount = e.getJiu_FengAmount();
                saveOrUpdateKpiDay("10040019", day, jiuFengAmount);
                saveOrUpdateStatistOtherParametersDay(4L, 40032L, day, jiuFengAmount);

                //总其他垃圾量
                String garbageOtherAmount = e.getGarbageOtherAmount();
                saveOrUpdateKpiDay("10040020", day, garbageOtherAmount);
                saveOrUpdateStatistOtherParametersDay(4L, 40033L, day, garbageOtherAmount);

                //总厨余垃圾
                String kitchenWasteAmount = e.getKitchenWasteAmount();
                saveOrUpdateKpiDay("10040021", day, kitchenWasteAmount);
                saveOrUpdateStatistOtherParametersDay(4L, 40034L, day, kitchenWasteAmount);

                //总餐厨垃圾
                String garbageKitchenAmount = e.getGarbageKitchenAmount();
                saveOrUpdateKpiDay("10040022", day, garbageKitchenAmount);
                saveOrUpdateStatistOtherParametersDay(4L, 40035L, day, garbageKitchenAmount);


            });
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdateKpiDay(String code, String day, String value) {
        String pattern = "[0-9]*\\.?[0-9]*";
        if (StringUtils.isEmpty(value) || !value.matches(pattern)) {
            return;
        }
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDateTime dayOfMonthKey = LocalDate.parse(day, dateTimeFormatter).atStartOfDay();
        CtKpiDay byCodeAndDayOfMonthKey = masterKpiDayRepository.findByCodeAndDayOfMonthKey(code, dayOfMonthKey);
        if (byCodeAndDayOfMonthKey == null) {
            byCodeAndDayOfMonthKey = new CtKpiDay();
            byCodeAndDayOfMonthKey.setCode(code);
            byCodeAndDayOfMonthKey.setDayOfMonthKey(dayOfMonthKey);
            byCodeAndDayOfMonthKey.setLastUpdateTime(LocalDateTime.now());
            byCodeAndDayOfMonthKey.setDataType(code);

        }
        byCodeAndDayOfMonthKey.setCurrentValue(value);
        masterKpiDayRepository.save(byCodeAndDayOfMonthKey);
    }

    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdateStatistOtherParametersDay(Long objId, Long type, String day, String value) {
        String pattern = "[0-9]*\\.?[0-9]*";
        if (StringUtils.isEmpty(value) || !value.matches(pattern)) {
            return;
        }

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDateTime dayOfMonthKey = LocalDate.parse(day, dateTimeFormatter).atStartOfDay();
        StatistOtherParametersDay byObjIdAndTypeAndDayOfMonthKey = masterStatistOtherParametersDayRepository.findByObjIdAndTypeAndDayOfMonthKey(objId, type, dayOfMonthKey);
        if (byObjIdAndTypeAndDayOfMonthKey == null) {
            byObjIdAndTypeAndDayOfMonthKey = new StatistOtherParametersDay();
            byObjIdAndTypeAndDayOfMonthKey.setObjId(objId);
            byObjIdAndTypeAndDayOfMonthKey.setType(type);
            byObjIdAndTypeAndDayOfMonthKey.setDayOfMonthKey(dayOfMonthKey);
            byObjIdAndTypeAndDayOfMonthKey.setObjType(1L);
            byObjIdAndTypeAndDayOfMonthKey.setLastTime(LocalDateTime.now());
            byObjIdAndTypeAndDayOfMonthKey.setInAvgValue(Double.valueOf(value));
            masterStatistOtherParametersDayRepository.save(byObjIdAndTypeAndDayOfMonthKey);
        } else {
            masterStatistOtherParametersDayRepository.updateDay(Double.valueOf(value), objId, dayOfMonthKey, type);
        }


    }
}
